let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    levelId:null,
    levelList:[
      {
        name:'直推',
        level:1
      },
      {
        name:'间推',
        level:2
      }
    ],
    levelStatus:1,  // 默认选中状态
    clientData:[],
    clientType:'grade',
    firstLevelCount:0,//直属下级人数
    otherLevelCount:0,//间接下级人数
    page:1,
    totalPage:0,
    isLoadMore:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if((options.diviLevel ?? '') !== '') {
      this.setData({
        levelId:Number(options.diviLevel)
      })
    }
    this.clientData();
  },
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },
  onPullDownRefresh: function() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1
    })
    this.clientData();
  },
  onGradeChange(event) { //切换tab
    console.log(event);
    this.setData({
      levelStatus:event.detail.name,
      page:1
    })
    this.clientData();
  },
  clientData() {
    let urlStr = app.getNetAddresss("plugin.team-analysis.frontend.list.diviList");
    let json = {
      page: this.data.page,
      divi_level: this.data.levelId,
      level:this.data.levelStatus
    };
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        let list = res.data.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result == 1) {
          this.data.isLoadMore = true;
          this.data.totalPage = res.data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          this.setData({
            clientData : list,
            firstLevelCount: res.data.first_level_count	,
            otherLevelCount: res.data.other_level_count
          })
        } else {
          this.data.page = this.data.page - 1;
          this.isLoadMore = false;
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.team-analysis.frontend.list.diviList");
      let json = {
        page: this.data.page,
        divi_level: this.data.levelId,
        level: this.data.levelStatus
      };
      app._getNetWork({
        url: urlStr,
        data: json,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data.data;
          console.log(res);
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              clientData : this.data.clientData.concat(list),
            })
          } else {
            // console.log()
            this.data.page = this.data.page - 1;
            // this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})